package com.example.aibot.aiHelper.service;


import com.example.aibot.aiHelper.tools.InterviewQuestionTool;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCodeHelperFactory {

    // 如果使用myQwenChatModel 启用日志功能
//    @Resource
//    private ChatModel myQwenChatModel;

    @Resource
    private ChatModel qwenChatModel;
    @Resource
    private ContentRetriever contentRetriever;

    @Resource
    private McpToolProvider mcpToolProvider;

    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeService aiCodeService(){
        // 直接调用AiServices.create 就能够创建出AiCodeService这个Service接口的实现类 背后的原理基于反射
        // 用反射机制创建了一个实现接口的代理对象
        // return AiServices.create(AiCodeService.class, qwenChatModel);

        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        AiCodeService aiCodeHelperService = AiServices.builder(AiCodeService.class)
                .chatModel(qwenChatModel)
                .streamingChatModel(qwenStreamingChatModel) // 实现流式输出
                .chatMemory(chatMemory) // 提供会话记忆功能
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))    // 每个会话独立存储
                .contentRetriever(contentRetriever) // 提供内容检索器 RAG检索增强生成
                .tools(new InterviewQuestionTool() )    // 工具调用
                .toolProvider(mcpToolProvider) // MCP 工具调用
                .build();
        return aiCodeHelperService;
    }


}
